package cc.chinagps.gboss.common.util;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

public class DateTimeUtil {
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_FORMAT_SIMPLE = "yyMMddHHmmss";

    public static final String FULL_DATETIME_FORMAT = "yyyyMMddHHmmssSSS";

    public static LocalDateTime parseStringToDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }

    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();//beijing is GMT+8,UTC is GMT
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    public static long getTimestampOfDateTimeGMT8(LocalDateTime localDateTime) {
        ZoneId zone = TimeZone.getTimeZone("GMT+8").toZoneId();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    public static String getDateTimeAsString(long timestamp, String format) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    public static Timestamp toDBTimestamp(String dateTime, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return Timestamp.valueOf(LocalDateTime.parse(dateTime, df));
    }

    public static byte[] getCurrentTimeBCD() {
        Instant instant = Instant.now();
        ZoneId zoneId = TimeZone.getTimeZone("GMT").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATETIME_FORMAT_SIMPLE);
        String s = localDateTime.format(formatter);
        return ConvertUtil.str2bcd(s);
    }

    public static byte[] getCurrentTimeBCD8() {
        Instant instant = Instant.now();
        ZoneId zoneId = TimeZone.getTimeZone("GMT+8").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATETIME_FORMAT_SIMPLE);
        String s = localDateTime.format(formatter);
        return ConvertUtil.str2bcd(s);
    }

    public static String getCurrentTime(String format) {
        Instant instant = Instant.now();
        ZoneId zoneId = TimeZone.getTimeZone("GMT+8").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    public static void main(String[] args) {
        System.out.println(getTimestampOfDateTime(LocalDateTime.now()));
        System.out.println(getTimestampOfDateTimeGMT8(LocalDateTime.now()));
        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        System.out.println(getCurrentTime(FULL_DATETIME_FORMAT));
    }
}
